set seed 5181985

***Diff-in-diff analysis***
use "Sheriffs and Extremist Violence Diff-in-Diff Dataset.dta", clear

**Two-way fixed effects**

xtset fips year

xtreg violence_right dem_winner, fe cluster(state)

**Poisson**

xtpoisson violence_right dem_winner, fe vce(robust)

**Callaway and Sant'Anna doubly robust model for staggered treatment timing**

by fips (year), sort: gen byte indicator_firsttreat = sum(dem_winner) == 1  & sum(dem_winner[_n - 1]) == 0
gen firsttreat_year = .
replace firsttreat_year = year if indicator_firsttreat==1
by fips: egen firsttreat = max(firsttreat_year)
replace firsttreat = 0 if firsttreat== .

by fips (year), sort: gen byte indicator_firsttreat2 = sum(rep_winner) == 1  & sum(rep_winner[_n - 1]) == 0
gen firsttreat_year2 = .
replace firsttreat_year2 = year if indicator_firsttreat2==1
by fips: egen firsttreat2 = max(firsttreat_year2)
replace firsttreat2 = 0 if firsttreat2== .

encode state, gen(state_id)

*Treatment: Democrat*
csdid violence_right dem_winner, ivar(fips) time(year) gvar(firsttreat) notyet asinr agg(simple) cluster(state_id)

*Treatment: Republican*

csdid violence_right rep_winner, ivar(fips) time(year) gvar(firsttreat2) notyet asinr agg(simple) cluster(state_id)

**Event study/parallel trends analysis**
csdid violence_right dem_winner, ivar(fips) time(year) gvar(firsttreat) notyet asinr agg(event) cluster(state_id)
csdid_plot, graphregion(color(white)) xtitle(Periods to Treatment (Democratic Sheriff))


****************
***Robustness***
****************

use "Sheriffs and Extremist Violence Diff-in-Diff Dataset.dta", clear

***No more right-wing violence under constitutionalist sheriffs***

tab violence_right const_sh

***Equivalence analysis***

*Using one quarter SD as equivalence interval*
tostregress violence_right dem_winner i.fips i.year, eqvlevel(0.0298) relevance cluster(state)


*************************************
***Mechanisms Het Effects Analysis***
*************************************

***BY ELECTION COMPETITIVENESS***

use "Sheriffs and Extremist Violence Diff-in-Diff Dataset.dta", clear

*Generate variable for top quarter most competitive elections
gen competitive = 0
replace competitive = 1 if vote_margin<=0.1208488

**Two-way fixed effects**

xtset fips year

xtreg violence_right dem_winner if competitive==0, fe cluster(state)
xtreg violence_right dem_winner if competitive==1, fe cluster(state)

xtpoisson violence_right dem_winner if competitive==0, fe vce(robust)
xtpoisson violence_right dem_winner if competitive==1, fe vce(robust)

**Callaway and Sant'Anna doubly robust model for staggered treatment timing**

by fips (year), sort: gen byte indicator_firsttreat = sum(dem_winner) == 1  & sum(dem_winner[_n - 1]) == 0
gen firsttreat_year = .
replace firsttreat_year = year if indicator_firsttreat==1
by fips: egen firsttreat = max(firsttreat_year)
replace firsttreat = 0 if firsttreat== .

by fips (year), sort: gen byte indicator_firsttreat2 = sum(rep_winner) == 1  & sum(rep_winner[_n - 1]) == 0
gen firsttreat_year2 = .
replace firsttreat_year2 = year if indicator_firsttreat2==1
by fips: egen firsttreat2 = max(firsttreat_year2)
replace firsttreat2 = 0 if firsttreat2== .

encode state, gen(state_id)


*Treatment: Democrat*
csdid violence_right dem_winner if competitive==0, ivar(fips) time(year) gvar(firsttreat) notyet asinr agg(simple) cluster(state_id)
csdid violence_right dem_winner if competitive==1, ivar(fips) time(year) gvar(firsttreat) notyet asinr agg(simple) cluster(state_id)
